import {createRouter, createWebHistory} from 'vue-router';
import testModule from './modules/testModule'
import authRoutes from "./modules/authModule.ts";
import { useAuthStore } from '../stores/useAuthStore.ts'
import gameModule from "./modules/gameModule.ts"

const routes = [
    ...testModule,
    ...authRoutes,
    ...gameModule,
    {
        path: '/',
        name: 'index',
        component: () => import("../view/index.vue")
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, _, next) => {
    const publicPages = ['/login'] // 白名单页面
    const authRequired = !publicPages.includes(to.path) // 是否需要登录
    const authStore = useAuthStore() // 获取当前登录状态

    if (authRequired && !authStore.token) {
        // 需要登录但未登录，跳转到登录页
        next('/login')
    } else {
        // 正常进入目标页面
        next()
    }
})

export default router